﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data.Mobile
{
    public enum DateType { Day, Week, Month, Year, FromTo }

    public class SelectDate
    {
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }
        private DateType DateType { get; set; }

        public SelectDate(DateType type, DateTime fromDate, DateTime toDate)
        {
            this.DateType = type;
            switch (type)
            {
                case DateType.Day: SetDay(); break;
                case DateType.Week: SetWeek(); break;
                case DateType.Month: SetMonth(fromDate); break;
                case DateType.Year: SetYear(fromDate); break;
                case DateType.FromTo:
                    FromDate = fromDate;
                    ToDate = toDate;
                    break;
            }
        }

        private void SetDay()
        {
            FromDate = DateTime.Now;
            ToDate = DateTime.Now;
        }

        private void SetMonth(DateTime date)
        {
            FromDate = new DateTime(date.Year, date.Month, 1);
            DateTime tmp = FromDate.AddMonths(1);
            ToDate = tmp.AddDays(-1);
        }

        private void SetWeek()
        {
            DateTime tmp = DateTime.Now;
            switch (tmp.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    FromDate = DateTime.Now;
                    ToDate = DateTime.Now.AddDays(6);
                    break;
                case DayOfWeek.Tuesday:
                    FromDate = tmp.AddDays(-1);
                    ToDate = DateTime.Now.AddDays(5);
                    break;
                case DayOfWeek.Wednesday:
                    FromDate = tmp.AddDays(-2);
                    ToDate = DateTime.Now.AddDays(4);
                    break;
                case DayOfWeek.Thursday:
                    FromDate = tmp.AddDays(-3);
                    ToDate = DateTime.Now.AddDays(3);
                    break;
                case DayOfWeek.Friday: 
                    FromDate = tmp.AddDays(-4);
                    ToDate = DateTime.Now.AddDays(2);
                    break;
                case DayOfWeek.Saturday:
                    FromDate = tmp.AddDays(-5);
                    ToDate = DateTime.Now.AddDays(1);
                    break;
                case DayOfWeek.Sunday:
                    FromDate = tmp.AddDays(-6);
                    ToDate = DateTime.Now;
                    break;
            }
        }

        private void SetYear(DateTime date)
        {
            FromDate = new DateTime(date.Year, 1, 1);
            ToDate = new DateTime(date.Year, 12, 31);
        }
    }
}
